<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Commission_mdl extends MY_Model {

    public function __construct() {
        parent::__construct();
        $this->table = 'commission';
    }

    /**
     * 添加每一个级别的佣金
     * 此方法主要为主方法提供基础环境
     */
    private $order_id;   //订单表ID
    private $par_id; //项目ID
    private $mess; //原始消息
    private $group;  //所有权限
    private $top_money; //辅助计算，计算上一级价格（初始值为当前用户购买价格）
    public function set_comm($oid){
        $this->order_id = $oid;
        //其他MODEL
        $this->load->model('prices_mdl');
        $this->load->model('mess_mdl');
        $this->load->model('group_mdl');
        $this->load->model('users_mdl');
        $this->load->model('uinfo_mdl');
        $this->load->model('bill_mdl');

        //获取所有权限
        $group = $this->group_mdl->select();

        //获取订单表
        $this->db->where('id',$this->order_id);
        $this->db->where('status',2);
        $order = $this->order_mdl->get_one();

        if(empty($order)){
            die('没有这个订单');
        }
        $this->par_id = $order['parameters_id'];
        $this->mess = $order['remarks'];

        //获取用户权限
        $this->db->where('id',$order['user_id']);
        $this->db->select('id,pid,group_id');
        $user = $this->users_mdl->get_one();

        //获取当前用户初始价格
        $this->db->where('group_id',$user['group_id']);
        $this->db->where('par_id',$order['parameters_id']);
        $res_prices = $this->prices_mdl->get_one();
        $this->top_money = empty($res_prices) ? $order['price'] : $res_prices['price'];

        $ugroup = array();//当前用户权限详细参数
        foreach($group as $k => $v){
            if($v['id'] == $user['group_id']){
                $ugroup = $v;
                unset($group[$k]);
            }
        }

        foreach($group as $k => $v){
            if($v['condition'] <= $ugroup['condition']){
                unset($group[$k]);
            }
        }
        $this->group = $group;

        $r = $this->set_commission($user['pid']);

        if($r){
            return $user['id'];
        }else{
            return FALSE;
        }
    }


    private function set_commission($pid){
        //查询当前用户参数
        $this->db->where('id',$pid);
        $this->db->select('id,pid,group_id');
        $user = $this->users_mdl->get_one();

        $groups = $this->group;
        foreach($groups as $k => $v){
            if($v['id'] == $user['group_id']){

                $this->db->where('par_id',$this->par_id);
                $this->db->where('group_id',$v['id']);
                $res = $this->prices_mdl->get_one();
                //设置价格表的时候添加
                if(!empty($res) && $this->top_money - $res['price'] > 0){
                    $data = array(
                        'user_id' => $user['id'],
                        'money' => $this->top_money - $res['price'],
                        'order_id' => $this->order_id,
                        'create_time' => $_SERVER['REQUEST_TIME']
                    );
                    $this->create($data);

                    //写入用户消息
                    $mess = sprintf('您的下级用户%s，您获得的佣金为 %s 元，已加入您的余额,<a href="%s">查看</a>',$this->mess,$data['money'],site_url('order/oinfo?uid='.$this->order_id));
                    $this->mess_mdl->send_message($mess,$user['id']);
                    //用户余额更新
                    $this->db->where('id',$user['id']);
                    $this->db->set('money','money+'.$data['money'],FALSE);
                    $this->uinfo_mdl->update();
                    //添加进用户账单记录
                    $this->bill_mdl->add_bill($user['id'],$data['money'],"下级用户购买佣金");
                }
                $this->top_money = $res['price'];
                unset($groups[$k]);
            }
        }

        $this->group = $groups;

        if($user['pid'] > 0){
            return $this->set_commission($user['pid']);
        }else{
            return true;
        }
    }

}

/* End of file Commission_mdl.php */
/* Location: ./application/models/Commission_mdl.php */